package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.pojo.Item;
import com.hmall.item.constant.MqConstants;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 查询商品详情
     *
     * @param id
     * @return {@link Item}
     */
    @GetMapping("{id}")
    public Item selectItem(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }

    /**
     * 分页查询商品
     *
     * @param
     * @param
     * @return {@link PageDTO}
     */
    @GetMapping("list")
    /*public PageDTO<Item> list(ItemPageDTO itemPageDTO){
        IPage<Item> iPage = new Page(itemPageDTO.getPage(),itemPageDTO.getSize());
        //itemService.page(iPage);
        itemMapper.selectPage(iPage,null);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(iPage.getTotal());
        pageDTO.setList(iPage.getRecords());
        return pageDTO;
    }*/
    public PageDTO<Item> list(@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        IPage<Item> iPage = new Page(page,size);
        //itemService.page(iPage);
        itemMapper.selectPage(iPage,null);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(iPage.getTotal());
        pageDTO.setList(iPage.getRecords());
        return pageDTO;
    }

    /**
     * 增加商品
     *
     * @param item
     * @return {@link Item}
     */
    @PostMapping
    public String add(@RequestBody Item item){

        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,item.getId());
        return "OK";
    }

    /**
     * 商品上架、下架
     *
     * @return {@link Item}
     */
    @PutMapping("/status/{id}/{status}")
    public String status(@PathVariable Long id,
                       @PathVariable("status") Integer status
                       ){
        Item item = itemService.getById(id);
        if (item.getStatus() == 1){
            item.setStatus(2);
        }
        else {
            item.setStatus(1);
        }
        itemService.updateById(item);
        return "OK";
    }

    /**
     * 修改商品
     *
     * @param item
     * @return {@link String}
     */
    @PutMapping
    public String updateById(@RequestBody Item item){
        itemService.updateById(item);
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_INSERT_KEY,item.getId());
        return "OK";
    }

    /**
     * 根据id删除商品
     *
     * @param id
     * @return {@link String}
     */
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id){
        itemMapper.deleteById(id);
        rabbitTemplate.convertAndSend(MqConstants.HMALL_EXCHANGE,MqConstants.HMALL_DELETE_KEY,id);
        return "Ok";
    }

    /**
     * 扣减库存接口
     *
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void subStock(@PathVariable Long itemId,@PathVariable Integer num){
        Item item = itemService.getById(itemId);
        if (item.getStock() > 0 && (item.getStock() - num) >= 0) {
            item.setStock(item.getStock() - num);
            itemService.updateById(item);
            return;
        }else {
            throw new RuntimeException("库存不够!");
        }

    }

}
